package com.skirmisher.video.aliVideo.device;

import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vs.model.v20181212.*;
import com.skirmisher.video.aliVideo.BaseAliVideo;
import com.skirmisher.video.entity.video.NationalDeviceEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName AliDevice
 * @Description TODO 设备相关阿里操作
 * @Author hanwangxin
 * @Date 2020/11/7 4:50 下午
 * @Version 1.0
 */
@Service
public class AliDevice extends BaseAliVideo {


    /**
     * @Author hanwangxin
     * @Description //TODO 根据空间查询设备列表 * @Date 6:00 下午 2020/11/7
     * @Param [groupId]
     * @return void
     **/
    public List<DescribeDevicesResponse.Device> describeDevices(String groupId) throws ClientException {
        IAcsClient client = getClient();

        DescribeDevicesRequest request = new DescribeDevicesRequest();
        request.setGroupId(groupId);


        DescribeDevicesResponse acsResponse = client.getAcsResponse(request);
        List<DescribeDevicesResponse.Device> devices = acsResponse.getDevices();

        Long totalCount = acsResponse.getTotalCount();
        if (totalCount <= 20){
            return devices;
        }else {
            long page = totalCount / 20;
            if (page * 20 == totalCount){
                for (long i = 2 ; i <= page;i++){
                    DescribeDevicesRequest request2 = new DescribeDevicesRequest();
                    request2.setGroupId(groupId);
                    request2.setPageNum(i);
                    DescribeDevicesResponse acsResponse2 = client.getAcsResponse(request);
                    List<DescribeDevicesResponse.Device> devices2 = acsResponse.getDevices();
                    devices.addAll(devices2);
                }
            }else {
                for (long i = 2 ; i <= page+1;i++){
                    DescribeDevicesRequest request3 = new DescribeDevicesRequest();
                    request3.setGroupId(groupId);
                    request3.setPageNum(i);
                    DescribeDevicesResponse acsResponse3 = client.getAcsResponse(request);
                    List<DescribeDevicesResponse.Device> devices3 = acsResponse.getDevices();
                    devices.addAll(devices3);
                }
            }

            return devices;
        }

    }

    /**
     * @Author hanwangxin
     * @Description //TODO 根据设备id获取设备相关信息 * @Date 6:06 下午 2020/11/7
     * @Param [deviceId]
     * @return com.aliyuncs.vs.model.v20181212.DescribeDevicesResponse
     **/
    public DescribeDeviceResponse describeDevice(String deviceId) throws ClientException {
        IAcsClient client = getClient();

        DescribeDeviceRequest request = new DescribeDeviceRequest();
        request.setId(deviceId);

        DescribeDeviceResponse acsResponse = client.getAcsResponse(request);


        return acsResponse;

    }


    /**
     * @Author hanwangxin
     * @Description //TODO 创建设备 暂不实行 * @Date 6:08 下午 2020/11/7
     * @Param []
     * @return java.lang.String
     **/
    public String createDevice(NationalDeviceEntity device) throws ClientException {

        IAcsClient client = getClient();

        CreateDeviceRequest request = new CreateDeviceRequest();
        request.setName(device.getDeviceName());
        request.setDescription(device.getDescription());
        request.setGroupId(device.getGroupId());
        request.setType(device.getType());
        request.setGbId(device.getGbid());
        request.setPort(device.getPort());
        request.setUsername(device.getUserName());
        request.setPassword(device.getPassword());

        CreateDeviceResponse acsResponse = client.getAcsResponse(request);

        String id = acsResponse.getId();
        return id;
    }

    /**
     * @Author hanwangxin
     * @Description //TODO 开启设备 * @Date 6:09 下午 2020/11/7
     * @Param [deviceId]
     * @return void
     **/
    public void  startDevice(String deviceId) throws ClientException {
        IAcsClient client = getClient();

        StartDeviceRequest request = new StartDeviceRequest();
        request.setId(deviceId);

        client.getAcsResponse(request);
    }

    /**
     * @Author hanwangxin
     * @Description //TODO 关闭设备 * @Date 6:13 下午 2020/11/7
     * @Param [deviceId]
     * @return void
     **/
    public void stopDevice(String deviceId) throws ClientException {
        IAcsClient client = getClient();
        
        StopDeviceRequest request = new StopDeviceRequest();
        request.setId(deviceId);
        
        client.getAcsResponse(request);
        
    }

}
